package com.ddy.test.thread;

/**
 * Created by ddy on 2017/12/15.
 */
public class SyncDemo {
    public synchronized void test1() {
        int i = 5;
        while( i-- > 0) {
            System.out.println(Thread.currentThread().getName() + " : " + i);
            try {
                Thread.sleep(100);
            }
            catch (InterruptedException ie) {
            }
        }
    }

    public static synchronized void test2() {
        int i = 5;
        while( i-- > 0) {
            System.out.println(Thread.currentThread().getName() + " : " + i);
            try {
                Thread.sleep(100);
            }
            catch (InterruptedException ie) {
            }
        }
    }

    public static void main(String[] args) {
        final SyncDemo myt2 = new SyncDemo();
        Thread test1 = new Thread(  new Runnable() {
            public void run() {
                myt2.test1();
            }
        }, "test1"  );

        Thread test2 = new Thread(  new Runnable() {
            public void run() {
                SyncDemo.test2();
            }
        }, "test2"  );
        test1.start();
        test2.start();
//         TestRunnable tr=new TestRunnable();
//         Thread test3=new Thread(tr);
//         test3.start();
    }
}
